package com.jk.dmhomeprovider.service;

import com.jk.dmcommon.LybEntity.UserEntity;
import com.jk.dmcommon.wlentity.CityEntity;
import com.jk.dmcommon.wlentity.GoodsDetailEntity;
import com.jk.dmcommon.wlentity.SwiperEntity;
import com.jk.dmhomeprovider.dao.HomeDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 文件名称:HomeService
 *
 * @Description //TODO
 * @Date 2021/9/8 13:58
 * 作者 王乐
 * Copyright (c) 2021,  All Rights Reserved.</pre>
 */
@Service
public class HomeService {
    @Autowired
    private HomeDao homeDao;
    @Autowired
    private RedisTemplate redisTemplate;
    //首页各类商品查询
    public List<GoodsDetailEntity> getHomeGoods(GoodsDetailEntity goods) {
        List<GoodsDetailEntity> list = homeDao.getHomeGoods(goods);

        return list;
    }
    //轮播图查询
    public List<SwiperEntity> getSwiper() {
        List<SwiperEntity> list =  new ArrayList<>();
        Boolean boo = redisTemplate.hasKey("Swiper903");
        if(boo){
            list = (List<SwiperEntity>) redisTemplate.opsForHash().get("Swiper903","swiper");
        }else {
            list = homeDao.getSwiper();
            redisTemplate.opsForHash().put("Swiper903","swiper",list);
            redisTemplate.expire("swiper903",20, TimeUnit.MINUTES);
        }
        list = homeDao.getSwiper();
        return list;
    }
    //获取城市
    public String getCity() {
        List<CityEntity> list = homeDao.getCity();
        String str = "";
        for (CityEntity city:list) {
            str+="<span class=\"name-city\" data-spm=\"dselectcity&amp;clicktitle="+city.getCityName()+"\" title="+city.getCityId()+" onclick=\"obtainCity("+city.getCityId()+")\" >"+city.getCityName()+"</span>";
        }
        return str;
    }
    //热门城市回显
    public String hotCity() {
      List<CityEntity> list=homeDao.hotCity();
      String str="";
        for (CityEntity city:list) {
            str+="<span class=\"name-city\" data-spm=\"dselectcity&amp;clicktitle="+city.getCityName()+"\" title="+city.getCityId()+"  onclick=\"obtainCity("+city.getCityId()+")\" >"+city.getCityName()+"</span>";
        }
        return  str;
    }
    //获取登录信息
    public HashMap<String, Object> getLoginInfo() {
        UserEntity user = (UserEntity) redisTemplate.opsForHash().get("dmLoginInfo","loginInfo");
        HashMap<String,Object> map = new HashMap<String,Object>();
        if(user==null){
            map.put("loginInfo",300);
        }else{
            map.put("loginInfo",user);
        }
        return map;
    }
    //选中替换
    public String obtainCity(Integer cityid) {
        //加热度
        homeDao.addCityHot(cityid);
        CityEntity city=homeDao.obtainCity(cityid);
        String str="";
             str="<span class=\"name-city\" data-spm=\"dselectcity&amp;clicktitle="+city.getCityName()+"\" title="+city.getCityId()+"  onclick=\"obtainCity("+city.getCityId()+")\" >"+city.getCityName()+"</span>";
         return str;
         /*   String str=city.getCityName();*/
    }
    //退出登录
    public void logout() {
        redisTemplate.opsForHash().delete("dmLoginInfo","loginInfo");
    }
}
